package me.frank.tman.ui.stock;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import me.frank.tman.R;
import me.frank.tman.model.StockExchangeModel;
import me.frank.tman.util.Utils;

public class StockListAdapter extends RecyclerView.Adapter<StockListAdapter.ViewHolder> {

    private static final String TAG = StockListAdapter.class.getSimpleName();

    public interface OnItemClickListener {
        public void onItemClick(View view, StockExchangeModel record);
    }

    private OnItemClickListener mItemListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemListener = listener;
    }

    private List<StockExchangeModel> localDataSet;

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout root;
        public TextView name, code, price, cost, capitalization, profit, volume, exchangeTime;

        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View
            root = view.findViewById(R.id.stock_card_root);
            name = view.findViewById(R.id.tv_stock_name);
            code = view.findViewById(R.id.tv_stock_code);
            price = view.findViewById(R.id.tv_stock_price);
            cost = view.findViewById(R.id.tv_stock_cost);
            capitalization = view.findViewById(R.id.tv_stock_capitalization);
            profit = view.findViewById(R.id.tv_stock_profit);
            volume = view.findViewById(R.id.tv_stock_volume);
            exchangeTime = view.findViewById(R.id.tv_stock_exchange_time);
        }
    }

    public StockListAdapter() {
        localDataSet = new ArrayList<>();
    }

    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet String[] containing the data to populate views to be used
     *                by RecyclerView.
     */
    public StockListAdapter(List<StockExchangeModel> dataSet) {
        localDataSet = dataSet;
    }

    public void setList(List<StockExchangeModel> dataSet) {
        localDataSet = dataSet;
        notifyDataSetChanged();
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view, which defines the UI of the list item
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_stock, viewGroup, false);

        return new ViewHolder(view);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        // Get element from your dataset at this position and replace the
        // contents of the view with that element
        StockExchangeModel record = localDataSet.get(position);
        viewHolder.name.setText(record.name);
        viewHolder.code.setText(record.code);
        viewHolder.price.setText(String.format("%.3f", record.price));
        viewHolder.cost.setText(String.format("%.3f", record.cost));
        viewHolder.capitalization.setText(String.format("%.3f", record.capitalization));
        viewHolder.profit.setText(String.format("%.3f", record.profit));
        viewHolder.volume.setText(String.format("%d", record.volume));
        viewHolder.exchangeTime.setText(Utils.stampToDate(record.exchangeTime));

        if (record.profit > 0) {
            viewHolder.root.setBackgroundColor(Color.parseColor("#D7171C"));
        } else {
            viewHolder.root.setBackgroundColor(Color.parseColor("#228B22"));
        }

        viewHolder.root.setOnClickListener(v -> mItemListener.onItemClick(v, record));
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return localDataSet.size();
    }
}
